/*========= This creates App Fig 1: event study analysis btwn UR and MW and Poverty ==========*/
import delimited "${data2}mw_2020.csv",clear
tempfile 2020
save `2020'

use  "${data2}mw_state_annual.dta",clear
rename statefips state_fips
rename  mean_mw mw
merge m:1 state_fips using "${data2}state_geocodes.dta", nogen keep(3)

tempfile mw
save `mw'

keep if year ==2019
replace year = 2020
merge 1:1 state_name using `2020', nogen keep(3)
append using `mw'

merge 1:1 state_fips year using "${data2}miscstate.dta", nogen keep(1 3)
replace year = year +1
keep if year >=1984 & year<=2013
 keep state_fips year mw  unemprate
 merge 1:1 state_fips year using  ${data1}state_panel_mw_poverty_84, keep(1 3) keepusing(ur_high_edu) nogen

tab year
xtset state_fips year

local first 3
local last 3

// Lead/Lags for MW
gen mw_d1=d1.mw
local v mw_d1
forval f = `first'(-1)1 {
sort state_fips  year
gen EF`f' = F`f'.`v'
label var  EF`f' "-`f'"
replace EF`f' = 0 if EF`f'==.
if `f' ==`first' label var  EF`f' "-`f'+"
}

forval l = 0/`last'  {
gen EL`l' = L`l'.`v'
label var  EL`l' "`l'"
replace EL`l' = 0 if EL`l'==.
if `l' ==`last' label var  EL`l' "`l'+"
}
order EF* EL* , last
gsort state_fips  -year
by state_fips : replace EF`first' = sum(EF`first')
gsort state_fips  year
by state_fips : replace EL`last' = sum(EL`last')


// Lead/Lags for UR
gen ur_d1=d1.unemprate
local v ur_d1
forval f = `first'(-1)1 {
sort state_fips  year
gen urEF`f' = F`f'.`v'
label var  urEF`f' "-`f'"
replace urEF`f' = 0 if urEF`f'==.
if `f' ==`first' label var  urEF`f' "-`f'+"
}

forval l = 0/`last'  {
gen urEL`l' = L`l'.`v'
label var  urEL`l' "`l'"
replace urEL`l' = 0 if urEL`l'==.
if `l' ==`last' label var  urEL`l' "`l'+"
}

order urEF* urEL* , last
gsort state_fips  -year
by state_fips : replace urEF`first' = sum(urEF`first')
gsort state_fips  year
by state_fips : replace urEL`last' = sum(urEL`last')


// Leads/Lags for high skilled ur
replace ur_high_edu = ur_high_edu*100
gen ur2_d1=d1.ur_high_edu
local v ur2_d1
forval f = `first'(-1)1 {
sort state_fips  year
gen ur2EF`f' = F`f'.`v'
label var  ur2EF`f' "-`f'"
replace ur2EF`f' = 0 if ur2EF`f'==.
if `f' ==`first' label var  ur2EF`f' "-`f'+"
}

forval l = 0/`last'  {
gen ur2EL`l' = L`l'.`v'
label var  ur2EL`l' "`l'"
replace ur2EL`l' = 0 if ur2EL`l'==.
if `l' ==`last' label var  ur2EL`l' "`l'+"
}

order ur2EF* ur2EL* , last
gsort state_fips  -year
by state_fips : replace ur2EF`first' = sum(ur2EF`first')
gsort state_fips  year
by state_fips : replace ur2EL`last' = sum(ur2EL`last')


tempfile mw 
save `mw'



local stat summstat(N)
local append outreg, merge(r) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(3) `stat'  keep(tobacco21_treated ) nolegend



local  dem i.race##i.post2002 i.marst i.famsize i.nchild i.educ i.hispan##i.post2002 i.sex i.nchild##i.pre1988  i.race#i.pre1988
local  dem1 i.dem_race##i.post2002  i.dem_race#i.pre1988 i.dem_married i.dem_famsize ///
i.dem_nchild i.dem_nchild##i.pre1988 i.dem_educ i.dem_female i.dem_hisp##i.post2002


local  a1 year i.state_fips `dem'

local rhs1 age* stateeitcrate //Dube Cont


local j 1
local d 1


use  "${data1}march_regready_1984.dta", clear
replace ur_high_edu = ur_high_edu*100
replace contpov = contpov2
replace eitc = eitc*eitc_ref

keep if year<=2013

merge m:1 state_fips year using `mw'

	
cap drop under
gen under = contpov2 < `j'
local q = `j' * 100
di "Poverty threshold `q'"
replace EF1=0
replace urEF1=0
replace urEF2=0

reghdfe ur_high_edu EF* EL* `rhs1'  [pw=wtsupp] if demgroup`d'==1, a(`a1') cluster(state_fips)
est save "${est}ster/other/es_mw_ur_high", replace


local range yla(-1.25(0.5)1.25)
 estimates use "${est}ster/other/es_mw_ur"
  est store D
  		coefplot ///
  			(D, omitted keep(EF* EL*)  msize(small) recast(connected) lcolor(gs1) color(gs1) lwidth(thin) ciopts(recast(rcap)  lcolor(gs10) lwidth(thin))),  ///
  			vertical xline(3.5) yline(0) graphregion(color(white)) yla(, nogrid) ///
  		   `range' ///
         legend(off) nooffsets ///
  			///title("`lab', ITN<`j'" , color(black))  /// "spec `spec'"
  			ytitle("Estimated Effect of Change in MW on Unemployment Rate") xtitle("Years Since MW Increases")
  		graph export "${fig}event_studies/other/MW_UR.png", replace


 local range yla(-0.015(0.005)0.015)
 estimates use "${est}ster/other/es_ur_pov"
  est store D
  		coefplot ///
  			(D, omitted keep(urEF* urEL*)  msize(small) recast(connected) lcolor(gs1) color(gs1) lwidth(thin) ciopts(recast(rcap)  lcolor(gs10) lwidth(thin))),  ///
  			vertical xline(3.5) yline(0) graphregion(color(white)) yla(, nogrid) ///
  		   `range' ///
         legend(off) nooffsets ///
  			///title("`lab', ITN<`j'" , color(black))  /// "spec `spec'"
  			ytitle("Estimated Effect of Change in UR on Poverty") xtitle("Years Since UR Changes")
  		graph export "${fig}event_studies/other/UR_Pov.png", replace


		

local range yla(-1.25(0.5)1.25)
 estimates use "${est}ster/other/es_mw_ur_high"
  est store D
  		coefplot ///
  			(D, omitted keep(EF* EL*)  msize(small) recast(connected) lcolor(gs1) color(gs1) lwidth(thin) ciopts(recast(rcap)  lcolor(gs10) lwidth(thin))),  ///
  			vertical xline(3.5) yline(0) graphregion(color(white)) yla(, nogrid) ///
  		   `range' ///
         legend(off) nooffsets ///
  			///title("`lab', ITN<`j'" , color(black))  /// "spec `spec'"
  			ytitle("Estimated Effect of Change in MW on High Skilled Unemployment Rate") xtitle("Years Since MW Increases")
  		graph export "${fig}event_studies/other/MW_High_UR.png", replace


 local range yla(-0.015(0.005)0.015)
 estimates use "${est}ster/other/es_ur_pov"
  est store D
  		coefplot ///
  			(D, omitted keep(urEF* urEL*)  msize(small) recast(connected) lcolor(gs1) color(gs1) lwidth(thin) ciopts(recast(rcap)  lcolor(gs10) lwidth(thin))),  ///
  			vertical xline(3.5) yline(0) graphregion(color(white)) yla(, nogrid) ///
  		   `range' ///
         legend(off) nooffsets ///
  			///title("`lab', ITN<`j'" , color(black))  /// "spec `spec'"
  			ytitle("Estimated Effect of Change in High Skilled UR on Poverty") xtitle("Years Since UR Changes")
  		graph export "${fig}event_studies/other/High_UR_Pov.png", replace

		